<template>
  <el-dialog conter :title="title" :visible="dialogVisible" width="30%" :before-close="handleClose">
    <el-form size="mini" ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
      <el-form-item v-if="!isTop" label="父级名称">
        <el-input :value="parentData.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add } from '@/api/building'
export default {
  name: 'DialogRole',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    isTop: {
      type: Boolean,
      default: true,
    },
    parentData: {
      type: Object,
      default: () => {},
    },
    isEdit: {
      type: Boolean,
      default: false,
    },
    editData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      title: '',
      ruleForm: {
        name: '',
        description: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.ruleForm.pid = this.isTop ? 0 : this.parentData.id
          await add(this.ruleForm)
          //重置表单数据
          this.ruleForm = {
            name: '',
            description: '',
          }
          //使用正确的引用重置验证状态
          this.$refs.ruleForm.resetFields()
          // 关闭弹窗 并刷新列表
          this.$emit('refreshList')
          let message = this.isEdit ? '修改成功' : '添加成功'
          this.$message.success(message)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleCancel() {
      let dialogVisible = false
      this.$emit('handleCancel', dialogVisible)
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(() => {
          this.handleCancel()
        })
        .catch(() => {})
    },
  },
  watch: {
    isTop: {
      handler(newValue) {
        // this.title = newValue ? '添加小区' : '添加子节点'
        // if (this.isEdit) {
        //   this.title = newValue ? '编辑小区' : '编辑子节点'
        // }
        if (newValue && this.isEdit) {
          this.title = '修改小区'
        } else if (!newValue && this.isEdit) {
          this.title = '修改子节点'
        } else if (newValue && !this.isEdit) {
          this.title = '添加小区'
        } else if (!newValue && !this.isEdit) {
          this.title = '添加子节点'
        }
      },
      immediate: true,
    },
    isEdit: {
      handler(newValue) {
        if (newValue && this.isTop) {
          this.title = '修改小区'
        } else if (!newValue && this.isTop) {
          this.title = '添加小区'
        } else if (newValue && !this.isTop) {
          this.title = '修改子节点'
        } else if (!newValue && !this.isTop) {
          this.title = '添加子节点'
        }

        if (newValue) {
          this.ruleForm = JSON.parse(JSON.stringify(this.editData))
        }
      },
      immediate: true,
    },
  },
}
</script>
